<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addPermissionList(0, 1)">添加权限</el-button>
      <el-table :data="list" row-key="id">
        <el-table-column label="名称" prop="name"/>
        <el-table-column label="标识" prop="code"/>
        <el-table-column label="描述" prop="description"/>
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button size="mini" type="text" v-if="row.type === 1" @click="addPermissionList(row.pid, row.type)">添加</el-button>
            <el-button size="mini" type="text" @click="editPermissionList(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
      :title="showText"
      :visible.sync="dialogVisible"
      width="30%"
      @close="btnClick"
      >
      <!-- :before-close="handleClose" -->
      <el-form ref="formData" :model="formData" label-width="80px" :rules="rules"> 
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>

        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code"></el-input>
        </el-form-item>

         <el-form-item label="权限描述">
          <el-input type="textarea" v-model="formData.description"></el-input>
        </el-form-item>

        <el-switch
          v-model="formData.enVisible"
          inactive-text="权限开关"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-value="1"
          inactive-value="0"
          >
        </el-switch>
      </el-form>  
      <span slot="footer" class="dialog-footer">
        <el-button @click="btnClick">取 消</el-button>
        <el-button type="primary" @click="hClick">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import { delpermission, getpermission, addPermission, putPermission, updataPermission } from '@/api/permission'
import { arrTree } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      dialogVisible: false,
      isTitle: true,
      formData: {
        name: '', // 权限名字
        code: '', // 权限点标识
        description: '', // 权限点描述
        type: '', // 权限点类型
        pid: '', // 权限点父级id
        enVisible: 0, // 权限点开启状态
      },
       rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      },
    }
  },
  created() {
    this.loadPermission()
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  methods: {
    async loadPermission() {
      const res = await getpermission()
      console.log(res)
      const list = arrTree(res.data, 0)
      this.list = list
    },
    async addPermissionList(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.dialogVisible = true
    },
    async hClick() {
      if (this.formData.id){
        await putPermission(this.formData)
      } else {
        await addPermission(this.formData)
      }
      // console.log(this.formData)
      this.$message.success('添加成功')
      this.loadPermission()
      this.dialogVisible = false
    },
    btnClick() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 
        pid: '', // 
        enVisible: "0" // 默认关闭
      }
      this.$refs.formData.resetFields()
      this.dialogVisible = false
    },
    async editPermissionList(id){
      this.dialogVisible = true
      const res = await updataPermission(id)
      this.formData = res.data
    },
    async del(id) {
      await delpermission(id)
      this.$message.success('删除成功')
      this.loadPermission()
    } 
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}

.el-switch {
  margin-left: 20px;
  font-size: 16px;
}

.el-switch__label * {
    line-height: 1;
    font-size: 16px;
    display: inline-block;
}
</style>